package DAO;

import Entity.Manger;
import Entity.scTab;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class scTabDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码


    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(scTab in) {
        Result result = new Result();
        sql = "insert into scTab values(?,?,?,?,?,?,?,?)";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);

            cmmd.setString(1, in.getScNo());
            cmmd.setString(2, in.getScName());//传入参数的个数
            cmmd.setString(3, in.getScP());
            cmmd.setString(4, in.getScTel());
            cmmd.setString(5, in.getScRespon());
            cmmd.setString(6, in.getScPrice());
            cmmd.setString(7, in.getScTime());
            cmmd.setString(8, in.getScSpecial());

            cmmd.execute(); //执行sql语句
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(scTab in) {
        Result result = new Result();
        sql = "update scTab set scName = '" + in.getScName() + "',scP = '" + in.getScP() + "',scTel = '" + in.getScTel() + "',scRespon= '" + in.getScRespon() + "',scPrice= '" + in.getScPrice() + "',scTime= '" + in.getScTime() + "',scSpecial='" + in.getScSpecial() + "'  where scNo = '" + in.getScNo() + "'";
        try {


            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.execute();


        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;

    }

    public Result Delete(String out) {
        Result result = new Result();
        sql = "delete from scTab where scNo='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }

    public Result Querry(String x) {
        scTab scnese;
        Result result = new Result();
        List<scTab> list = new ArrayList<>();


        sql = "Select * from scTab where scNo like '%" + x + "%' order by scNo+''";

        try {

            System.out.println(sql + "111");
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            //cmmd.setString(1,x);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                scnese = new scTab();
                scnese.setScNo(rs.getString(1));
                scnese.setScName(rs.getString(2));
                scnese.setScP(rs.getString(3));
                scnese.setScTel(rs.getString(4));
                scnese.setScRespon(rs.getString(5));
                scnese.setScPrice(rs.getString(6));
                scnese.setScTime(rs.getString(7));
                scnese.setScSpecial(rs.getString(8));
                list.add(scnese);
            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;
    }
    public String justgetID() throws SQLException {
        String id = null;
        sql = "SELECT scNo FROM scTab order by (scNo+0) DESC limit 1";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            rs = cmmd.executeQuery();
            while (rs.next()){
                id = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }

    //通过景区名查重
    public boolean exit_Name(String name){
        sql = " select count(*) from scTab where scName = ?";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.setString(1, name);
            rs = cmmd.executeQuery();
            while (rs.next()) {
                if (rs.getInt(1) > 0) {
                    JOptionPane.showMessageDialog(null, "此景区已存在，请检查输入");
                    return false;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return true;
    }

    //通过精确查找ID
    public scTab get_byID(String id){
        scTab scnese = new scTab();;
        sql = "Select * from scTab where scNo = '" + id + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                scnese.setScNo(rs.getString(1));
                scnese.setScName(rs.getString(2));
                scnese.setScP(rs.getString(3));
                scnese.setScTel(rs.getString(4));
                scnese.setScRespon(rs.getString(5));
                scnese.setScPrice(rs.getString(6));
                scnese.setScTime(rs.getString(7));
                scnese.setScSpecial(rs.getString(8));

            }
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,"没有获取到信息");
        }
        return scnese;
    }
}
